package com.liwei.easyweibo.activity;



import com.liwei.easyweibo.R;
import com.liwei.easyweibo.api.WeiboShare;
import com.liwei.easyweibo.util.AndroidUtil;
import com.liwei.easyweibo.util.Constant;
import com.liwei.easyweibo.util.Util;
import com.tencent.weibo.oauthv2.OAuthV2;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class WriteWeiboActivity extends Activity implements OnClickListener{
	private final static String TAG = "HomeActivityWeibo";
	private EditText mContent;
	private Button mBtnOk,mBtnCamera;
	private WeiboShare mWeibo;
	
	private ImageView mImageView;
	private Bitmap bmp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		mWeibo = new WeiboShare(this);
	}
	
	private void initView(){
		setContentView(R.layout.writeweibo);
		mContent = (EditText) findViewById(R.id.et_mycontent);
		//再次启动的时候清空
		mContent.setText("");
		
		//发微博的按钮事件
		mBtnOk 	= (Button) findViewById(R.id.btn_send);
		mBtnOk.setOnClickListener(this);
		
		//拍照
		mBtnCamera = (Button) findViewById(R.id.btn_camera);
		mBtnCamera.setOnClickListener(this);
		
		mImageView = (ImageView) findViewById(R.id.iv_load);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.menu_about:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.menu_about);
			builder.setMessage(R.string.about_message);
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			builder.show();
			break;
		case R.id.menu_manage:
			Intent i = new Intent();
			i.setClass(this, BindWeiboActivity.class);
			this.startActivity(i);
			break;
		case R.id.menu_settings:
			break;
		}
		return true;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_send:
			boolean isBindToSina = Util.isBindToSina(WriteWeiboActivity.this);
			OAuthV2 oauth = Util.readTencentFromPreference(WriteWeiboActivity.this);
//			boolean isBindToWechat = Util.readBFromPreference(WriteWeiboActivity.this, Constant.SHAREDPRE_WECHAT);
			
			if(isBindToSina || oauth != null){
				// 直接走发送微博的流程
				String content  = mContent.getText().toString();
				 mWeibo.setmContent(content);
				
				 if(isBindToSina){
				  mWeibo.shareToPlatform(Constant.TYPE_WEIBO_SINA);
				}
				
				 //腾讯微博
				if(oauth != null){
					mWeibo.shareToPlatform(Constant.TYPE_WEIBO_TENCENT);
				}
				
//				//微信不再支持了
//				if(isBindToWechat){
//					mWeibo.shareToPlatform(Constant.TYPE_WECHAT);
//				}
				
			}else{
				//跳转到绑定微博的页面
				Intent i = new Intent();
				i.setClass(WriteWeiboActivity.this, BindWeiboActivity.class);
				WriteWeiboActivity.this.startActivity(i);
			}
			break;
		case R.id.btn_camera:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.upload_image);
			builder.setItems(R.array.menu_array, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					switch(which){
					case 0:
						//从本地选择图片
						Intent i = new Intent();
						i.setType("image/*");
						i.setAction(Intent.ACTION_GET_CONTENT);
						WriteWeiboActivity.this.startActivityForResult(i, Constant.IMAGE_OPEN);
						break;
					case 1:
						//拍照
						// create Intent to take a picture and return control to the calling application
						Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
						Uri fileUri = Util.getOutputMediaFileUri(Constant.MEDIA_TYPE_IMAGE); // create a file to save the image
						intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
						//存放图片的路径
						String mImagePath = fileUri.getPath();
						
						//由于在onActivityForResult中拿不到值，所以将Imagepath放到shareprefarence中去
						Util.writeToPreference(WriteWeiboActivity.this, Constant.SHARE_IMAGE_PATH, mImagePath);
						startActivityForResult(intent, Constant.IMAGE_SAVE);
						break;
					}
				}
			});
			builder.show();
			break;
		}
	}
	
	
	private long showTime = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		/**
		 * 为了连续单击两次退出
		 * */
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			long currTime 	= System.currentTimeMillis();
			if(currTime - showTime > 2000){
				Util.showToast(this, R.string.back_true,Toast.LENGTH_SHORT);
			}else{
				return super.onKeyDown(keyCode, event);
			}
			showTime =System.currentTimeMillis();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
		if(requestCode == Constant.IMAGE_SAVE){
			if(resultCode == RESULT_OK){
				String mImagePath = Util.readSFromPreference(this, Constant.SHARE_IMAGE_PATH);
				if(mImagePath != null){
					mWeibo.setmPcFilePath(mImagePath);
					Util.d(TAG, "mImagePath=" +mImagePath);
					//发送图片
					bmp = AndroidUtil.getBitmapFromFile(mImagePath, 48, 48);
					mImageView.setImageBitmap(bmp);
				}
			}
		}else if(requestCode == Constant.IMAGE_OPEN){
			if(resultCode == RESULT_OK){
				Uri uri = data.getData();
				if(uri != null){
					String imagePath = "";
					if(uri.toString().startsWith("content")){
						imagePath = AndroidUtil.getFilePathFromUri(this, uri);
					}else{
						imagePath = uri.getPath();
					}
					Util.d(TAG, "uri=" +imagePath);
					mWeibo.setmPcFilePath(imagePath);
					
					//发送图片
					bmp = AndroidUtil.getBitmapFromFile(imagePath, 48, 48);
					mImageView.setImageBitmap(bmp);
				}
			}
		}
	}
}
